package com.nf.xiaomi.dao.impl;

import com.nf.dbutils.SqlExecutor;
import com.nf.dbutils.handlers.BeanListHandler;
import com.nf.dbutils.handlers.ScalarHandler;
import com.nf.xiaomi.dao.OrderDao;
import com.nf.xiaomi.entity.OrderEntity;
import com.nf.xiaomi.util.SqlExecutorUtils;

import java.util.List;

public class OrderDaoImpl implements OrderDao {
    //未付款--1
    //待发货（发货）--2
    //待收货--3
    //已完成--4
    SqlExecutor executor = SqlExecutorUtils.getExecutor();

    public List<OrderEntity> getAll() {
        String sql = "select Cast(GROUP_CONCAT(product_detail_id) as char)detail_id_list,order_id,user_account,user_name,user_phone,\n" +
                "address_content,order_mode,SUM(product_detail_amount)sum_amount,SUM(product_detail_price)sum_price,\n" +
                "order_date,nstate\n" +
                "from(select od.product_detail_id,o.order_id,u.user_account,a.user_name,a.user_phone,a.address_content\n" +
                "      ,o.order_mode,od.product_detail_amount,od.product_detail_price,o.order_date,o.nstate from order_info o\n" +
                "               left join order_detail od on o.order_id = od.order_id\n" +
                "                left join user_info u on o.user_id = u.user_id\n" +
                "                left join address a on u.address_id = a.address_id) order_info GROUP BY order_id";
        return executor.query(sql, new BeanListHandler<>(OrderEntity.class));
    }

    public List<OrderEntity> getAll(Integer orderState) {
        String sql = "select Cast(GROUP_CONCAT(product_detail_id) as char)detail_id_list,order_id,user_account,user_name,user_phone,\n" +
                "address_content,order_mode,SUM(product_detail_amount)sum_amount,SUM(product_detail_price)sum_price,\n" +
                "order_date,nstate\n" +
                "from(select od.product_detail_id,o.order_id,u.user_account,a.user_name,a.user_phone,a.address_content\n" +
                "      ,o.order_mode,od.product_detail_amount,od.product_detail_price,o.order_date,o.nstate from order_info o\n" +
                "               left join order_detail od on o.order_id = od.order_id\n" +
                "                left join user_info u on o.user_id = u.user_id\n" +
                "                left join address a on u.address_id = a.address_id) order_info GROUP BY order_id\n" +
                "HAVING nstate = ?";
        return executor.query(sql, new BeanListHandler<>(OrderEntity.class),orderState);
    }

    public List<OrderEntity> getAll(Long account) {
        String sql = "select Cast(GROUP_CONCAT(product_detail_id) as char)detail_id_list,order_id,user_account,user_name,user_phone,\n" +
                "address_content,order_mode,SUM(product_detail_amount)sum_amount,SUM(product_detail_price)sum_price,\n" +
                "order_date,nstate\n" +
                "from(select od.product_detail_id,o.order_id,u.user_account,a.user_name,a.user_phone,a.address_content\n" +
                "      ,o.order_mode,od.product_detail_amount,od.product_detail_price,o.order_date,o.nstate from order_info o\n" +
                "               left join order_detail od on o.order_id = od.order_id\n" +
                "                left join user_info u on o.user_id = u.user_id\n" +
                "                left join address a on u.address_id = a.address_id) order_info GROUP BY order_id\n" +
                "HAVING user_account = ?";
        return executor.query(sql, new BeanListHandler<>(OrderEntity.class), account);
    }

    public Integer insert(OrderEntity order) {
        String sql = "insert into order_info(order_id,user_id,order_mode,nstate)values(?,?,?,?)";
        return executor.update(sql,order.getOrderID(),order.getUser_id(),order.getOrderMode(),order.getState());
    }

    public Integer update(Integer userID,Integer orderID,Integer state) {
        String sql = "update order_info set nstate = ? where user_id = ? and order_id =?";
        return executor.update(sql, state, userID, orderID);
    }

    public Integer deleteByID(Integer userID, Integer orderID) {
        String sql = "delete from order_info where user_id = ? and order_id =?";
        return executor.update(sql, userID, orderID);
    }

    public List<OrderEntity> total() {
        String sql = "select count(order_id) from order_info ";
        BeanListHandler<OrderEntity> handler = new BeanListHandler<>(OrderEntity.class);
        return executor.query(sql, handler);
    }

    public List<OrderEntity> confirmOrder(Long account, Integer orderID) {
        String sql = "select Cast(GROUP_CONCAT(product_detail_id) as char)detail_id_list,order_id,user_account,user_name,user_phone,\n" +
                "address_content,order_mode,SUM(product_detail_amount)sum_amount,SUM(product_detail_price)sum_price,\n" +
                "order_date,nstate\n" +
                "from(select od.product_detail_id,o.order_id,u.user_account,a.user_name,a.user_phone,a.address_content\n" +
                "      ,o.order_mode,od.product_detail_amount,od.product_detail_price,o.order_date,o.nstate from order_info o\n" +
                "               left join order_detail od on o.order_id = od.order_id\n" +
                "                left join user_info u on o.user_id = u.user_id\n" +
                "                left join address a on u.address_id = a.address_id) order_info GROUP BY order_id\n" +
                "HAVING user_account = ? and order_id = ?";
        BeanListHandler<OrderEntity> listHandler = new BeanListHandler<>(OrderEntity.class);
        return executor.query(sql, listHandler, account, orderID);
    }

    public Long paymentCount(Integer userID) {
        String sql = "select count(*) as num from order_info where user_id=? and nState = 1";
        return executor.query(sql, new ScalarHandler<Long>(), userID);
    }

    public Long goodsCount(Integer userID) {
        String sql = "select count(*) as num from order_info where user_id=? and nState = 3";
        return executor.query(sql, new ScalarHandler<Long>(), userID);
    }

    public Long recycleCount(Integer userID) {
        String sql = "select count(*) as num from order_info where user_id=? and nState = 4";
        return executor.query(sql, new ScalarHandler<Long>(), userID);
    }
}
